import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { SimpleTableColumn, SimpleTableComponent } from '@delon/abc';
import { NzMessageService, NzModalService, NzTabChangeEvent, UploadFile, NzNotificationService } from 'ng-zorro-antd';
import { CommService } from 'app/service/comm/comm.service';
import { Constant, ServiceResult } from '@core/constant';
import { EmitterService } from 'app/service/emit.service';
import { Router } from '@angular/router';
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { TranslateService } from '@ngx-translate/core';
import { Timeout } from '@shared/descriptor/timeout';
import {maListService} from "../../../service/maList/maList.service";
import {blListService} from "../../../service/blList/blList.service";
@Component({
  selector: 'blList02-list',
  templateUrl: './blList02-list.component.html',
  styleUrls: ['./blList-list.component.less']
})
export class blList02Component implements OnInit, OnDestroy {

  // region 公共字段

  visible = false;
  // pageSize
  ps = 10;
  // 搜索表单
  q: any = {
    blXuhao:"",
    lifnr:"",
    name1: '',
    blKuanhao: '',
    blPinpai: '',
    blPinxiang: '',
    blSrmx13: '',
  };

  loading = false;
  // 表格组件
  @ViewChild('st') st: SimpleTableComponent;
  // 表格数据url
  url = this.blListService.list02();
  // region end

  // 搜索区域显示/隐藏标记
  // 显示图片
  previewVisible = false;
  // 大图地址
  previewImage = '';
  imgSuffix = '';

  years = [];
  userGroups = [];
  brands = [];
  seasons = [];
  checkedList = [];
  data = [];
  uploadExcelUrl = this.blListService.uploadcarinfo02();

  uploadfile = this.blListService.uploadfile();

  uploadFile(info: { file: UploadFile }) {
    if (info.file.status === 'uploading') {
      this.loading = true;
    }
    if (info.file.status === 'done') {
      this.loading = false;
      console.log("info1*********"+JSON.stringify(info.file.response[0]));
      this.notice.create('success', '成功', info.file.response[0].fileUrl);
    }
    if (info.file.status === 'error') {
      this.loading = false;
      this.notice.create('error', '失败', '上传失败');
    }
  }
  columns: SimpleTableColumn[] = [
    { title: '', index: 'key', type: 'checkbox' },

    // {
    //   title: '操作',
    //   buttons: [
    //     {
    //       text: '修改',
    //       type: 'link',
    //       click: (record: any) => this.router.navigate(['/blList/blList/edit/'+record.id])
    //     }
    //   ]
    // } ,
    // {"备料序号", "供应商编号", "供应商名称", "品牌", "品项",
    //   "款号","备料或消耗","工厂","原材备料","备半成品","当前总备料","实际原材到仓数","实际半成品","未到仓数","实际总备料数","通知与实际差异","备注"};
    { title: '备料序号', index: 'blXuhao' },
    { title: '审批状态', index: 'blSrmx13' },

    { title: '供应商编码', index: 'lifnr' },
    { title: '供应商名称', index: 'name1'},
    { title: '采购类型', index: 'blSrmx23' },
    { title: '采购性质', index: 'blPinxiang' },
    { title: '物料号', index: 'blKuanhao' },
    { title: '备料或消耗', index: 'blSrmx1' },
    { title: '工厂', index: 'blSrmx2' },
    // { title: '原材备料', index: 'blSrmx3' },
    // { title: '备半成品', index: 'blSrmx4' },
    { title: '备料数', index: 'blSrmx5' },
    // { title: '实际原材到仓数', index: 'blSrmx6' },
    // { title: '实际半成品', index: 'blSrmx7' },
    // { title: '未到仓数', index: 'blSrmx8' },
    // { title: '实际总备料数', index: 'blSrmx9' },
    // { title: '通知与实际差异', index: 'blSrmx10' },
    { title: '备注', index: 'blSrmx11' },
    { title: '附件地址', index: 'blSrmx12' },
    { title: '理论交期', index: 'blSrmx14' },
    { title: '需求日期', index: 'blSrmx15' },
    { title: '承诺交期', index: 'blSrmx16' },
    { title: '实际交期', index: 'blSrmx17' },
    { title: '备料物料号', index: 'blSrmx20' },
    { title: '备料物料描述', index: 'blSrmx21' }



  ];

  constructor(
    private trans: TranslateService,
    public http: _HttpClient,
    public msg: NzMessageService,
    private modalSrv: ModalHelper,
    private comm: CommService,
    private cnt: Constant,
    private emit: EmitterService,
    private router: Router,
    private modalService: NzModalService,
    private blListService: blListService,
    private notice: NzNotificationService,
  ) {}
  syncing = false;
  lastDate = null;

  ngOnDestroy() {
    if (this.inter) {
      clearInterval(this.inter);
    }
  }
  inter
  ngOnInit() {
    this.imgSuffix = this.comm.getGoodsUrl();
    this.emit.getMessage().subscribe(data => {
      if (data.text === 'RELOAD_GOODS_LIST') {
        this.q.blSrmx13="未审批";
        this.st.load(1, this.q);
      }
    })
  }
  fullChange(val: boolean) {
    // this.scroll = val ? { y: '350px' } : { y: '230px' };
  }
  checkboxChange(list: any[]) {
    this.checkedList = list;
  }

  loaddata(wqtype){
    // console.log(wqtype);
    // this.q.bpmStatus=wqtype;
    this.q.blSrmx13="未审批";

    if(1==wqtype){
      this.q.blSrmx13="未审批";
    }
    if(2==wqtype){
      this.q.blSrmx13="已审批";
    }
    this.st.load(1,this.q)
  }

  downurl='';
  packattamb(){
    this.loading = true;

    let conmapList = [];

    try{
      let dto1 ={
        conKey:"lmatnr",
        conValue:this.q.lmatnr,
      }
      conmapList.push(dto1);

      let dto2 ={
        conKey:"matnr",
        conValue:this.q.matnr,
      }
      conmapList.push(dto2);

      let dto3 ={
        conKey:"lifnr",
        conValue:this.q.lifnr,
      }

      conmapList.push(dto3);

      let dto4 ={
        conKey:"name1",
        conValue:this.q.name1,
      }

      conmapList.push(dto4);
      let dto5 ={
        conKey:"blXuhao",
        conValue:this.q.blXuhao,
      }

      conmapList.push(dto5);
    }catch (e) {

    }
    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genblmb(this.q.lifnr,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }
  packatta(){
    this.loading = true;

    let conmapList = [];

    try{
      let dto1 ={
        conKey:"lmatnr",
        conValue:this.q.lmatnr,
      }
      conmapList.push(dto1);

      let dto2 ={
        conKey:"matnr",
        conValue:this.q.matnr,
      }
      conmapList.push(dto2);

      let dto3 ={
        conKey:"lifnr",
        conValue:this.q.lifnr,
      }

      conmapList.push(dto3);

      let dto4 ={
        conKey:"name1",
        conValue:this.q.name1,
      }

      conmapList.push(dto4);
      let dto5 ={
        conKey:"blXuhao",
        conValue:this.q.blXuhao,
      }

      conmapList.push(dto5);
    }catch (e) {

    }
    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genbl(this.q.lifnr,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }

  shenpi(){
    this.loading = true;

    let conmapList = [];

    try{
      let dto1 ={
        conKey:"lmatnr",
        conValue:this.q.lmatnr,
      }
      conmapList.push(dto1);

      let dto2 ={
        conKey:"matnr",
        conValue:this.q.matnr,
      }
      conmapList.push(dto2);

      let dto3 ={
        conKey:"lifnr",
        conValue:this.q.lifnr,
      }

      conmapList.push(dto3);

      let dto4 ={
        conKey:"name1",
        conValue:this.q.name1,
      }

      conmapList.push(dto4);

    }catch (e) {

    }
    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genbl(this.q.lifnr,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }


  xiadan(){
    this.loading = true;

    let conmapList = [];

    try{
      let dto1 ={
        conKey:"lmatnr",
        conValue:this.q.lmatnr,
      }
      conmapList.push(dto1);

      let dto2 ={
        conKey:"matnr",
        conValue:this.q.matnr,
      }
      conmapList.push(dto2);

      let dto3 ={
        conKey:"lifnr",
        conValue:this.q.lifnr,
      }

      conmapList.push(dto3);

      let dto4 ={
        conKey:"name1",
        conValue:this.q.name1,
      }

      conmapList.push(dto4);

    }catch (e) {

    }
    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genbl(this.q.lifnr,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }
  opennewwindow(url){

    const newWindow = window.open(url, '_blank');
    newWindow.document.title = '下载';

  }

  close(): void {
    this.visible = false;
  }
  cancel() {
    this.repdate = null;
  }
  repdate = null;
  uploadProd(info: { file: UploadFile }) {
    if (info.file.status === 'uploading') {
      this.loading = true;
    }
    if (info.file.status === 'done') {
      this.loading = false;
      console.log("info1*********"+JSON.stringify(info.file.response[0]));
      this.st.load();
      this.notice.create('success', '成功', info.file.response[0].piccontent);
    }
    if (info.file.status === 'error') {
      this.loading = false;
      this.notice.create('error', '失败', '导入失败');
    }
  }
}
